package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getFindTrademarkListId(Long category3Id) {
        LambdaQueryWrapper<BaseCategoryTrademark> longLambdaQueryWrapper = new LambdaQueryWrapper<>();
        longLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseMapper.selectList(longLambdaQueryWrapper);
        //判断是否为空  不为空进行 遍历
        if (!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            List<Long> list = baseCategoryTrademarks.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(list);
            return baseTrademarkList;

        }
        return null;
    }
    /**
     * 根据分类ID查询该分类未关联品牌
     *
     * @param category3Id 三级分类ID
     * @return
     */
    @Override
    public List<BaseTrademark> getFindCurrentTrademarkList(Long category3Id) {
        //
        List<BaseTrademark> baseTrademarkList   =     baseMapper.getFindCurrentTrademarkList(category3Id);
        return baseTrademarkList;
    }
    /**
     * 保存分类品牌关联
     * @param vo 三级分类编号 和 品牌id列表
     * @return
     */
    @Override
    public void saveCategoryTrademarkVo(CategoryTrademarkVo vo) {
        Long category3Id = vo.getCategory3Id();
        List<Long> trademarkIdList = vo.getTrademarkIdList();

        //判断是否有新增的品牌ID
        if (!CollectionUtils.isEmpty( trademarkIdList)) {
           BaseCategoryTrademark baseCategoryTrademark= new BaseCategoryTrademark();
            List<BaseCategoryTrademark> categoryTrademarksList = trademarkIdList.stream().map((trademarkId) -> {
                baseCategoryTrademark.setTrademarkId(trademarkId);
                baseCategoryTrademark.setCategory3Id(category3Id);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //存储
            super.saveBatch(categoryTrademarksList);
        }

    }
    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void delete(Long category3Id, Long trademarkId) {
        //逻辑删除
//        LambdaQueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
//        baseCategoryTrademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
//        baseMapper.delete(baseCategoryTrademarkLambdaQueryWrapper);
        //物理删除
        baseMapper.deleteCategoryAndTrademark(category3Id,trademarkId);
    }

}
